Date.prototype.format = function(fmt) {
  let o = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'h+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    'S': this.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substring(4 - RegExp.$1.length)) }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substring(('' + o[k]).length)))
    }
  }
  return fmt
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成 2018/09/23 11:54:16
 * @param datetime 国际化日期格式
 */
export function format(datetime) {
    return formatWithSeperator(new Date(datetime), "/", ":");
}

/**
 * 时间格式化
 * 将 2018-09-23T11:54:16.000+0000 格式化成类似 2018/09/23 11:54:16
 * 可以指定日期和时间分隔符
 * @param dateMat
 * @param dateSeprator
 * @param timeSeprator
 */
export function formatWithSeperator(dateMat, dateSeprator, timeSeprator) {
    if (dateMat != null) {
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const hh = dateMat.getHours();
        const mm = dateMat.getMinutes();
        const ss = dateMat.getSeconds();
        return year + dateSeprator + month + dateSeprator + day + " " + hh + timeSeprator + mm + timeSeprator + ss;
    }
}

/**
 * 时间格式化
 * 将 毫秒格式转换为 yyyy-MM-dd HH:mm
 * 可以指定日期和时间分隔符
 * @param dateTime
 * @param type
 */
export function simpleDateFormat(dateTime, type) {
    let d = new Date(dateTime);
    let month = d.getMonth() + 1;
    let day = d.getDate();
    let year = d.getFullYear();
    let hh = d.getHours();
    let mm = d.getMinutes();
    let ss = d.getSeconds();
    if (month < 10){
        month = '0' + month;
    }
    if (day < 10){
        day = '0' + day;
    }
    if (hh < 10){
        hh = '0' + hh;
    }
    if (mm < 10){
        mm = '0' + mm;
    }
    if (ss < 10){
        ss = '0' + ss;
    }
    let timeFormat = '';
    if (type === 'yyyy-MM-dd') {
        timeFormat = year + '-' + month + '-' + day;
    } else if(type === 'yyyy-MM-dd HH:mm'){
        timeFormat = year + '-' + month + '-' + day +' '+ hh + ":" + mm;
    } else if(type === 'yyyy-MM-dd HH:mm:ss'){
        timeFormat = year + '-' + month + '-' + day +' '+ hh + ":" + mm + ":" + ss;
    } else if(type === 'yyyy年MM月dd日'){
        timeFormat = year + '年' + month + '月' + day + '日';
    } else if(type === 'yyyyMMdd'){
        timeFormat = year + "" + month + "" + day;
    } else if(type === 'yyyy-MM'){
      timeFormat = year + '-' + month;
    }
    return timeFormat;
}

/**
 * 获取当前日期，格式：2020-01-01
 */
export function getCurrDate() {
    return formatDate(new Date());
}

/**
 * 获取当前日期，格式：2020-01-01 12:10:10
 */
export function getCurrTime() {
    let d = new Date();
    let month = d.getMonth() + 1;
    let day = d.getDate();
    let year = d.getFullYear();
    let hh = d.getHours();
    let mm = d.getMinutes();
    let ss = d.getSeconds();
    if (month < 10){
        month = '0' + month;
    }
    if (day < 10){
        day = '0' + day;
    }
    if (hh < 10){
        hh = '0' + hh;
    }
    if (mm < 10){
        mm = '0' + mm;
    }
    if (ss < 10){
        ss = '0' + ss;
    }
    return year + '-' + month + '-' + day +' '+ hh + ":" + mm + ":" + ss;
}

/**
 * 获取上一个月，格式：2020-01
 */
export function getLastMonth() {
    let dateMat = new Date();
    let year = dateMat.getFullYear();
    let month = dateMat.getMonth();
    if (month <= 0) {
        year = year - 1;
        month = 12;
    }
    return year + "-" + (month < 10 ? "0" : "") + month;
}

/**
 * 获取上一个月，格式：2020-01
 */
export function getLastMonthNoSplit() {
    let dateMat = new Date();
    let year = dateMat.getFullYear();
    let month = dateMat.getMonth();
    if (month <= 0) {
        year = year - 1;
        month = 12;
    }
    return year + "" + (month < 10 ? "0" : "") + month;
}

/**
 * 获取当前日期上(负数)/下(正数)一秒的时间，格式：2020-01-01
 */
export function getNextDate(second) {
    return formatDate(new Date(new Date().getTime() + second * 1000));
}

export function formatDate(dateMat) {
    let year = dateMat.getFullYear();
    let month = dateMat.getMonth() + 1;
    let day = dateMat.getDate();

    return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day;
}

/**
 * 获取2个日期相关天数
 * @param startDate
 * @param endDate
 * @returns {number}
 */
function getDiffDay(startDate, endDate) {
    const startMilliSecond = Date.parse(startDate);
    const endMilliSecond = Date.parse(endDate);
    const milliSecond = endMilliSecond - startMilliSecond;
    const left = milliSecond % (24 * 3600 * 1000)
    const hours = Math.floor(left / (3600 * 1000))
    return Math.floor(milliSecond / (24 * 60 * 60 * 1000)) + (hours > 0 ? 0.5 : 0);
}

export default {
    getCurrDate,
    getCurrTime,
    getNextDate,
    getLastMonth,
    getLastMonthNoSplit,
    simpleDateFormat,
    getDiffDay
}



